//
//  SearchTableViewCell.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/26.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "SearchTableViewCell.h"

#define W self.contentView.frame.size.width
#define H self.contentView.frame.size.height
@interface SearchTableViewCell ()
@property (nonatomic, retain) UIImageView *dishImageView;
@property (nonatomic, retain) UILabel *dishNameLabel;
@property (nonatomic, retain) UILabel *descriptionLabel;
@property (nonatomic, retain) UIImageView *hardLevelIcon;
@property (nonatomic, retain) UILabel *hardLevelLabel;
@property (nonatomic, retain) UIImageView *tasteImageIcon;
@property (nonatomic, retain) UILabel *tasteLabel;
@property (nonatomic, retain) UIImageView *cookTimeIcon;
@property (nonatomic, retain) UILabel *cookTimeLabel;
@end
@implementation SearchTableViewCell

- (void)dealloc
{
    [_dishImageView release];
    [_dishNameLabel release];
    [_descriptionLabel release];
    [_hardLevelIcon release];
    [_tasteImageIcon release];
    [_tasteLabel release];
    [_cookTimeIcon release];
    [_cookTimeLabel release];
    [_dishModel release];
    [_key release];
    [super dealloc];
}

#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        /*
         *  dishImageView
         */
        // 初始化
        self.dishImageView = [[UIImageView alloc] init];
        
        // 将dishImageView加入self
        [self addSubview:self.dishImageView];
        
        // 内存管理
        [_dishImageView release];
        
        /*
         *  dishNameLabel
         */
        // 初始化
        self.dishNameLabel = [[UILabel alloc] init];
        
        // 将dishNameLabel加入self
        [self addSubview:self.dishNameLabel];
        
        // 内存管理
        [_dishNameLabel release];
        
        
        
        // descriptLabel
        self.descriptionLabel = [[UILabel alloc] init];
        [self addSubview:self.descriptionLabel];
        self.descriptionLabel.numberOfLines = 2 * (W /375);
        self.descriptionLabel.font = [UIFont systemFontOfSize:15];
        self.descriptionLabel.textColor = [UIColor lightGrayColor];
        [_descriptionLabel release];
        
        
        //  hardLevelIcon
        self.hardLevelIcon = [[UIImageView alloc] init];
        self.hardLevelIcon.image = [UIImage imageNamed:@"cooker"];
        [self addSubview:self.hardLevelIcon];
        [_hardLevelIcon release];
        
        
        //  hardLevelLabel
        self.hardLevelLabel = [[UILabel alloc] init];
        [self addSubview:self.hardLevelLabel];
        self.hardLevelLabel.font = [UIFont systemFontOfSize:10];
        self.hardLevelLabel.textColor = [UIColor darkGrayColor];
        [_hardLevelLabel release];
        
        
        //  tasteImageIcon
        self.tasteImageIcon = [[UIImageView alloc] init];
               [self addSubview:self.tasteImageIcon];
        [_tasteImageIcon release];
        
        
        //  tasteLabel
        
        self.tasteLabel = [[UILabel alloc] init];
        self.tasteLabel.font = [UIFont systemFontOfSize:10];
        self.tasteLabel.textColor = [UIColor darkGrayColor];
        [self.contentView addSubview:self.tasteLabel];
        [_tasteLabel release];
        
        /*
         //  cookTimeIcon
         */
        // 初始化
        self.cookTimeIcon = [[UIImageView alloc] init];
        
               // 将cookTimeIcon加入self
        [self addSubview:self.cookTimeIcon];
        
        // 内存管理
        [_cookTimeIcon release];
        
        /*
         *  cookTimeLabel
         */
        // 初始化
        self.cookTimeLabel = [[UILabel alloc] init];
        
        // 字体大小
        self.cookTimeLabel.font = [UIFont systemFontOfSize:10];
        
        // 字体颜色
        self.cookTimeLabel.textColor = [UIColor darkGrayColor];
        
        // 将cookTimeLabel加入self
        [self addSubview:self.cookTimeLabel];
        
        // 内存管理
        [_cookTimeLabel release];
    }
    return self;
}
-(void)layoutSubviews
{
// 调用父类布局方法
[super layoutSubviews];

    //  设置dishImageView的frame
    self.dishImageView.frame = CGRectMake(10 * (W / 375), 10 * (W / 375), W * 0.4, H - 20);
    
    //  设置dishNameLabel的frame
    self.dishNameLabel.frame = CGRectMake(W * 0.5, 10 * (W / 375), W * 0.5 - 20, H * 0.2);
    
    //  设置descriptionLabel的frame
    self.descriptionLabel.frame = CGRectMake(W * 0.5, H * 0.15 + 10 * (W / 375), W * 0.5 - 20, H * 0.3);
    
    //  设置hardLevelIcon的frame
    self.hardLevelIcon.frame = CGRectMake(W * 0.5, H * 0.55 + 10 * (W / 375), W * 0.05, H * 0.1);
    
    //  设置hardLevelLabel的frame
    self.hardLevelLabel.frame = CGRectMake(W * 0.56, H * 0.5 + 10 * (W / 375), W * 0.2, H * 0.2);
    
    //  设置tasteImageIcon的frame
    self.tasteImageIcon.frame = CGRectMake(W * 0.75, H * 0.5 + 17 * (W / 375), W * 0.05, H * 0.1);
    
    //  设置tasteLabel的frame
    self.tasteLabel.frame = CGRectMake(W * 0.8, H * 0.5 + 10 * (W / 375), W * 0.1, H * 0.2);
    
    //  设置cookTimeIcon的frame
    self.cookTimeIcon.frame = CGRectMake(W * 0.5, H * 0.7 + 17 * (W / 375), W * 0.05, H * 0.1);
    
    //  设置cookTimeLabel的frame
    self.cookTimeLabel.frame = CGRectMake(W * 0.56, H * 0.7 + 10 * (W / 375), W * 0.3, H * 0.2);
    
}

- (void)setDishModel:(SearchModel *)dishModel
{
    // retain类型赋值
    if (_dishModel != dishModel) {
        [_dishModel release];
        _dishModel = [dishModel retain];
    }
    
    // dishImageView赋值
    [self.dishImageView sd_setImageWithURL:[NSURL URLWithString:dishModel.image]];
    
    
    //得到关键字位置
    NSMutableAttributedString *dishNameAttributeString = [[NSMutableAttributedString alloc] initWithString:dishModel.title];
    NSRange range = [dishModel.title rangeOfString:self.key];
    
    // 生成高亮字符串
    [dishNameAttributeString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.93 green:0.25 blue:0.21 alpha:1] range:range];
    
   
    // dishNameLabel赋值
    self.dishNameLabel.attributedText = dishNameAttributeString;
    
    
    // discriptLabel赋值
    self.descriptionLabel.text = dishModel.descript;
    
    // hardLevelLabel赋值
    self.hardLevelLabel.text = [NSString stringWithFormat:@"难度: %@", dishModel.hard_level];
    
    // tasteLabel赋值
    self.tasteLabel.text = [NSString stringWithFormat:@"口味: %@", dishModel.taste];
    
    // cookTimeLabel赋值
    self.cookTimeLabel.text = [NSString stringWithFormat:@"烹饪时间: %@", dishModel.cookie];
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
